﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.Numerical.Quadrature
{
	public class GaussLobattoChebyshev<T> : IGaussQuadrature<T, T>
	{

		readonly List<T> _nodes;
		readonly T _normalWeight;
		readonly T _firstAndLastWeight;
		readonly CommonCollection.FunctionBasedList<T> _weights;

		public GaussLobattoChebyshev(int order)
		{
			List<T> temp = new List<T>();
			for (int i = 0; i <= order; i++)
			{
				temp.Add(DefaultValueProvider<T>.GetValueFromDouble(-1 * Math.Cos(((double)i) / order * Math.PI)));
			}
			this._nodes = new List<T>(temp);

			//
			_normalWeight = DefaultValueProvider<T>.GetValueFromDouble(Math.PI / Order);
			_firstAndLastWeight = DefaultValueProvider<T>.GetValueFromDouble(Math.PI / Order * 0.5);
			//
			_weights = new CommonCollection.FunctionBasedList<T>
			{
				Getter = (_index) =>
				{
					if (_index == 0 || _index == Order)
					{
						return _firstAndLastWeight;
					}
					else
					{
						return _normalWeight;
					}
				},
				GetCount = () => Order + 1
			};
		}

		#region IGaussQuadrature<T,T> Members

		public int Order
		{
			get { return _nodes.Count - 1; }
		}
		public IList<T> Nodes
		{
			get
			{
				return this._nodes;
			}
		}
		public IList<T> Weights
		{
			get
			{
				return this._weights;
			}
		}

		#endregion
	}
}
